var cashed = false;

$(document).ready(function () {
    var p = new Pagination();

    $("#approve-btn").click(function () {
        $(".nav-tabs li").removeClass("active");
        $(this).parent().addClass("active");
        cashed = false;
        genPage();
    });

    $("#cashed-btn").click(function () {
        $(".nav-tabs li").removeClass("active");
        $(this).parent().addClass("active");
        cashed = true;
        genPage();
    });

    genPage();

    function genPage() {
        Util.ajax({
            type: "POST",
            data: {cashed: cashed},
            url: "./rest/admin/cashlistcount",
            success: function (data) {
                if (data.status != 0) {
                    return Util.errorData("分页信息获取失败");
                }

                p.init(0, data.data, loadCashlist);
            }
        })
    };

    function loadCashlist() {
        Util.ajax({
            type: "POST",
            data: {offset: p.getOffset(), len: p.getLen(), cashed: cashed},
            url: "./rest/admin/cashlist",
            success: function (data) {
                if (data.status!=0)
                    return Util.errorData("信息获取失败");

                if (cashed)
                    var render = _.template($("#tmpl-cashed").html());
                else
                    var render = _.template($("#tmpl-approve").html());
                var str = "";
                _.each(data.data, function (d) {
                    d.applytime = new Date(d.applytime).format("yyyy-MM-dd hh:mm");
                    str += render(d);
                });

                $("#cash-list").html(str);
            }
        });
    };
});